﻿package com.cse.htmlparser;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.htmlparser.*;
import org.htmlparser.http.ConnectionManager;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.ParserException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.w3c.dom.Element;


import com.cse.index.ProductIndexer;
import com.cse.service.BaseService;

public abstract class AbstractParser {
	protected Logger logger = Logger.getLogger(AbstractParser.class);  
	
	protected Parser parser ;
	
	protected ConnectionManager manager ;
	
	protected ProductIndexer indexer ;
	
	//用于保存网页的path
	protected String htmlpath ;
	
	//用于保存map的路径
	protected String filepath;
	
	
	//用于保存 品牌-url 的map
	protected Map<String,String> map;
	
	
	public AbstractParser(String html,String file){
		try{
			
			map = new HashMap<String, String>();
			indexer = ProductIndexer.getIndexer();
			htmlpath = html;
			filepath = file;
		}catch(Exception exception){
			logger.error(exception);
		}
	}
	
	/* -------------------------------------
	 * 负责打开连接
	 */
	private void openConnection(String url){

		manager = Page.getConnectionManager();
		try {
			parser = new Parser(manager.openConnection(url));
			
			parser.setEncoding("gbk");
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
	}
	
	
	/*
	 * 负责装载文档
	 */
	private void loadFile(String path){
		try {
			parser = new Parser(path);
			parser.setEncoding("gbk");
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * 负责对传入的html直接进行创建parser
	 */
	private void loadHtml(String html){
		try{
			logger.info("创建的是htmlparser");
			parser = Parser.createParser(html, "gbk");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	//初始化 parser
	protected void initialize(String destpath){
		if(destpath.indexOf("http") == 0){
			openConnection(destpath);
		}else if(destpath.indexOf("http") > 0){
			loadHtml(destpath);
		}else
			loadFile(destpath);
		
	}
	
	
	/**
	 * 处理一种分类下 所有品牌的链接并保存到一个map当中去
	 * @param path
	 */
	//解析文档的方法
	protected abstract void extract();
	
	
	
	/**
	 * 处理每一个品牌下面的所有类型
	 * 
	 * @param brand 品牌
	 * @param url 
	 */
	protected abstract void extractbrand(String brand,String url);
	
	
	
	/**
	 * 处理该map下所有的品牌
	 * 
	 */
	protected abstract void extractall();
	
	
	public void startExtract(){
		extract();
		extractall();
	}
	
	
	@SuppressWarnings("unchecked")
	protected Map<String,String> getMap(){
		File inFile = new File(filepath); 
		Object o = null; 
		
		try { 
		ObjectInputStream in = new ObjectInputStream( 
		new BufferedInputStream(new FileInputStream(inFile))); 
		o = in.readObject(); 
		in.close(); 
		} catch (Exception e) { 
			System.out.println(e); 
		} 
		Map<String, String> o2 = (Map<String, String>) o;
		return o2; 
	} 
	
	
	
	protected  void WriteFile(){
		try{
			ObjectOutputStream out = new ObjectOutputStream( 
					new BufferedOutputStream(new FileOutputStream(filepath))); 
			out.writeObject(this.map); 
			out.close(); 
			} catch (Exception e) { 
			System.out.println(e); 
		}
	}
	
	public static void main(String[] args){
		String http = "http://wangnn09.blog.163.com/blog/static/1362508732011228112346265/";
		String file = ".\\html\\jingdongphone.htm";
		String html = "<!DOCTYPE html PUBLIC";
		int index = html.indexOf("http");
		System.out.println(index);
	}
	
}
